﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IocpSharp.Socks5;
using System.IO;
using System.Net;


namespace ClanSocketProxy
{
    public partial class Form1 : Form
    {
        Socks5Server server;
        private DateTime startTime;
        private List<UserCredentials> userCredentialsList;

        public Form1()
        {
            
            server = new Socks5Server(AppDomain.CurrentDomain.BaseDirectory + "pac.lst");
            InitializeComponent();
            startTime = DateTime.Now;

            //
            System.Console.SetOut(new Server_Frm.CSS.TextBoxWriter(trackBox));

            // 初始化用户和密码列表
            userCredentialsList = new List<UserCredentials>
            {
                new UserCredentials("silenceyj", "yyj159357"),
                new UserCredentials("user", "password")
            };

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            // 获取用户名和密码
            string username = textBox1.Text;
            string password = textBox2.Text;

            // 登录
            if (IsValidLogin(username, password))
            {
                // 认证成功，弹出成功消息框
                MessageBox.Show("认证成功!");

                try
                {
                    string ip = ipBox.Text;
                    int port = int.Parse(portBox.Text);
                    server.Start(ip, port);
                    Console.WriteLine("服务器启动成功，监听地址：" + server.LocalEndPoint.ToString());
                    //writeLine("服务器启动成功，监听地址：" + server.LocalEndPoint.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    writeLine(ex.ToString());
                }

                Console.ReadLine();
            }
            else
            {
                // 认证失败，弹出失败消息框
                MessageBox.Show("认证失败，请检查用户名和密码。");
            }
            
        }

        public void writeLine(String str)
        {
            trackBox.Text += str;
            trackBox.Text += "\n";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label4.Text = "";
            // 获取本机的 IP 地址列表
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

            // 显示本机的第一个 IPv4 地址
            foreach (IPAddress ipAddress in localIPs)
            {
                if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    label4.Text = "本机IP地址：" + ipAddress.ToString();
                    break;
                }
            }
            if (string.IsNullOrEmpty(label4.Text))
            {
                label4.Text = "无法获取本机IP地址";
            }
            // 获取本机主机名
            string hostName = Dns.GetHostName();
            label4.Text += Environment.NewLine + "本机主机名：" + hostName;
            // 显示程序启动时间
            label4.Text += Environment.NewLine + "程序启动时间：" + startTime.ToString();
        }


 

        private bool IsValidLogin(string username, string password)
        {
            // 在用户和密码列表中查找匹配项
            foreach (var credentials in userCredentialsList)
            {
                if (credentials.Username == username && credentials.Password == password)
                {
                    return true; // 找到匹配项，认证成功
                }
            }

            return false; // 用户名或密码不匹配
        }

        public class UserCredentials
        {
            public string Username { get; set; }
            public string Password { get; set; }

            public UserCredentials(string username, string password)
            {
                Username = username;
                Password = password;
            }
        }

    }

   

}
